package com.sunky.rfidapp.V2

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import com.sunky.db.model.CheckRecord
import com.sunky.db.model.EquipInfoV2
import com.sunky.db.util.DBManager
import com.sunky.db.util.updateRecord
import com.sunky.rfidapp.BaseActivity
import com.sunky.rfidapp.R
import com.sunky.rfidapp.util.CommonUtil
import com.sunky.rfidapp.util.setTextValid
import kotlinx.android.synthetic.main.activity_equip_info_detail_v2.*
import kotlinx.android.synthetic.main.activity_equip_info_detail_v2.view.*
import kotlinx.android.synthetic.main.item_equip_locations.view.*
import kotlinx.android.synthetic.main.layout_bottom_check.*
import kotlinx.android.synthetic.main.layout_title.*
import java.text.SimpleDateFormat
import java.util.*

class EquipDetailActivityV2 : BaseActivity() {
    private var checkRecord = CheckRecord()

    var equipInfo: EquipInfoV2? = null

    override fun handleIntent(intent: Intent) {
        equipInfo = CommonUtil.GlobalStorage["EquipDetailActivity"] as EquipInfoV2
        CommonUtil.GlobalStorage.remove("EquipDetailActivity")

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_equip_info_detail_v2
    }

    override fun initView() {
        TvTitle.text = "装备点验"
        LayoutCheckEquip.visibility = View.VISIBLE
        var id = equipInfo?._id
        var serialNo = equipInfo?.equipSerial
        TvNormal.setOnClickListener{
            checkRecord.id = id?:0
            checkRecord.serialNo = serialNo
            checkRecord.state = "1"
            checkRecord.datetime = getFormatDateTime()
            DBManager.getInstance().updateRecord(checkRecordDao,checkRecord)
            toast("点验完成")
            setResult(RESULT_OK)
            finish()
        }
        TvBad.setOnClickListener {
            checkRecord.id = id?:0
            checkRecord.serialNo = serialNo
            checkRecord.state = "0"
            checkRecord.datetime = getFormatDateTime()
            DBManager.getInstance().updateRecord(checkRecordDao,checkRecord)
            toast("点验完成")
            setResult(RESULT_OK)
            finish()
        }
    }

    override fun initData() {
        equipInfo?.apply {
            tvEquipTypeName.setTextValid(equipTypeName)
            tvCodeInfo.setTextValid(codeinfo)
            tvEquipName.setTextValid(equipName)
            tvEquipCategory.setTextValid(equipCategory)
            tvEquipProducer.setTextValid(equipProducer)
            tvEquipCode.setTextValid(equipCode)
            tvManager.setTextValid(responsePerson)
            tvProducerYear.setTextValid(producerYear)
            tvBatchNo.setTextValid(producerNo)
            tvEquipProperty.setTextValid(equipPropertyname)
            tvSaveDate.setTextValid(saveTime)
            tvSaveCondition.setTextValid(saveCondition)
            tvQualityChange.setTextValid(qualityChange)
                addItem(this)
//            tvCount.setTextValid(totalCount.toString())
            tvFunction.setTextValid(mainFunction)
            tvQuato.setTextValid(mainQuato)
//            Observable.create<List<EquipInfoV2>> {
//                it.onNext(DBManager.getInstance().getList(equipDaoV2, WhereCondition.StringCondition("EQUIP_TYPE_NAME = ? and EQUIP_NAME = ?",equipTypeName,equipName))!!)
//            }.subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe({
//                        tvEquipTypeName.setTextValid(equipTypeName)
//                        tvCodeInfo.setTextValid(codeinfo)
//                        tvEquipName.setTextValid(equipName)
//                        tvEquipCategory.setTextValid(equipCategory)
//                        tvEquipProducer.setTextValid(equipProducer)
//                        tvEquipCode.setTextValid(equipCode)
//                        var totalCount = 0
//                        it.iterator().forEach { info->
//                            addItem(info)
//                            try {
//                                totalCount += Integer.parseInt(info.equipCount)
//                            }catch (e:Exception){
//                            }
//                        }
//                        tvCount.setTextValid(totalCount.toString())
//                        tvFunction.setTextValid(mainFunction)
//                        tvQuato.setTextValid(mainQuato)
//                    },{
//                        toast("数据查询失败")
//                        it.printStackTrace()
//                    })
        }

    }

    private fun addItem(info: EquipInfoV2){
        LayoutInflater.from(this).inflate(R.layout.item_equip_locations,null).apply {
            LayoutEquipLocations.addView(this)
            tvEquipSerial.setTextValid(info.equipSerial)
            tvUnitName.setTextValid(info.unitName)
            tvEquipCount.setTextValid(info.equipCount)
            tvSavePosition.setTextValid(info.savePosition)
            tvUseYear.setTextValid(info.useYear)
            tvQualityStatename.setTextValid(info.qualityStatename)
            tvNowStatename.setTextValid(info.nowStatename)
        }
    }

    private fun getFormatDateTime():String{
        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(Date())
    }
}